﻿using IceRpc;
using SageKingIceRpc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace SageKing.IceRPC.Extensions;

public static class StreamPackageDataExts
{
    public static StreamPackage GetStreamPackage(this StreamPackageData data, int serverNo, Dictionary<string, string> headDic, int errNo = 0, string errInfo = "")
    {
        return new StreamPackage()
        {
            ServiceNo = serverNo,
            Type = (byte)StreamPackageTypeEnum.SageKingMessage,
            ErrorNo = errNo,
            ErrorInfo = errInfo,
            ClientId = string.Empty,
            MsgId = string.Empty,
            Uuid = string.Empty,
            HeadDic = headDic ?? new Dictionary<string, string>(),
            DataStreamRowType = data.RowType,
            DataStreamBody = data.DataBody.ToArray()
        };
    }

    public static ISageKingMessage GetStreamPackageData(this StreamPackage data, Dictionary<DataStreamTypeEnum, Dictionary<string, byte>> postDic)
    {
        if (data.Type != (byte)StreamPackageTypeEnum.SageKingMessage)
        {
            throw new Exception($"入参数类型不正确({data.Type}->{(byte)StreamPackageTypeEnum.SageKingMessage})");
        }
        var result = new SageKingMessage();
        result.InitAttribytePos(postDic);
        result.LoadData(new StreamPackageData(data.DataStreamRowType.ToArray(), data.DataStreamBody.ToArrayTwo()));
        return result;
    }
}
